package com.xci.sys.controller;

import com.xci.core.annotation.Authorize;
import com.xci.core.base.RestMessage;
import com.xci.core.base.WebController;
import com.xci.core.exceptions.EntityNotFoundException;
import com.xci.core.helper.ConvertHelper;
import com.xci.core.helper.ExcelHelper;
import com.xci.core.helper.Helper;
import com.xci.core.web.JsonGrid;
import com.xci.sys.entity.SysDept;
import com.xci.sys.entity.SysModule;
import com.xci.sys.entity.SysUser;
import com.xci.sys.filter.RoleFilter;
import com.xci.sys.filter.UserFilter;
import com.xci.sys.service.DeptService;
import com.xci.sys.service.ModuleService;
import com.xci.sys.service.RoleService;
import com.xci.sys.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统用户控制器
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/sys/user")
public class UserController extends WebController {
    @Resource
    private UserService userService;//用户服务
    @Resource
    private DeptService deptService;//部门服务
    @Resource
    private RoleService roleService;//角色服务
    @Resource
    private ModuleService moduleService;//模块服务

    //region 页面视图

    /**
     * 用户首页
     */
    @GetMapping
    @Authorize(code = "SystemUser")
    public String index() {
        return "sys/user/index";
    }

    /**
     * 用户新建页面
     */
    @GetMapping("/create")
    @Authorize(code = "SystemUser.Create")
    public String create(ModelMap map) {
        createMark(map);
        SysUser entity = new SysUser();
        entity.setId(Helper.nextIdStr());
        entity.setStatus(true);
        map.put("entity", entity);
        var roleFilter = new RoleFilter();
        roleFilter.setStatus(true);
        roleFilter.setDeptId(getCurrentUser().getDeptId());
        var roleSimples = roleService.selectList(roleFilter);
        map.put("roleSimples",roleSimples);
        return "sys/user/edit";
    }

    /**
     * 用户编辑页面
     */
    @GetMapping("/edit")
    @Authorize(code = "SystemUser.Edit")
    public String edit(String id, ModelMap map) {
        SysUser entity = userService.selectById(id);
        if (entity == null) throw new EntityNotFoundException(id);

        map.put("entity", entity);
        var roleFilter = new RoleFilter();
        roleFilter.setStatus(true);
        roleFilter.setDeptId(getCurrentUser().getDeptId());
        var roleSimples = roleService.selectList(roleFilter);
        map.put("roleSimples",roleSimples);
        return "sys/user/edit";
    }

    /**
     * 用户详细页面
     */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        SysUser entity = userService.selectById(id);
        if (entity == null)  throw new EntityNotFoundException(id);

        map.put("entity", entity);
        // map.put("roles", permissionService.queryUserMapRoles(id));
        // map.put("selfDataSettingDesc", AuthHelper.getObjectDataSettingDesc(permissionService.queryObjectDataSetting(id)));
        // map.put("ownDataSetting", AuthHelper.mergeDataSetting(permissionService.queryUserOwnDataSettings(entity)));
        return "sys/user/details";
    }

    /**
     * 用户拥有的模块 tree 节点
     *
     * @param id 用户主键
     */
    @ResponseBody
    @GetMapping("/user-own-modules")
    public RestMessage userOwnModules(String id) {
        SysUser user = userService.selectById(id);
        List<SysModule> modules = moduleService.selectUserModuleList(user);
        return RestMessage.tree(moduleService.convertToNodeList(modules));
    }

    /**
     * 用户拥有的部门 tree 节点
     *
     * @param id 用户主键
     */
    @ResponseBody
    @GetMapping("/user-own-departments")
    public RestMessage userOwnDepartments(String id) {
        SysUser user = userService.selectById(id);
        List<SysDept> departments = roleService.selectDeptMapList(user.getDeptId());
        return RestMessage.tree(deptService.convertToNodeList(departments));
    }

    /**
     * 修改密码页面
     */
    @GetMapping("/modify-password")
    public String modifyPassword() {
        return "sys/user/modify-password";
    }

    //endregion

    //region 数据处理

    /**
     * 用户表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize(code = "SystemUser.Search")
    public JsonGrid grid(UserFilter filter) {
        return RestMessage.grid(userService.selectList(filter));
    }

    /**
     * 保存用户
     */
    @ResponseBody
    @PostMapping("/create-save")
    @Authorize(code = "SystemUser.Create")
    public RestMessage createSave(@ModelAttribute SysUser entity) {
        return userService.insert(entity);
    }

    /**
     * 保存用户
     */
    @ResponseBody
    @PostMapping("/edit-save")
    @Authorize(code = "SystemUser.Edit")
    public RestMessage editSave(@ModelAttribute SysUser entity) {
        return userService.update(entity);
    }

    /**
     * 修改用户启用状态
     */
    @ResponseBody
    @PostMapping("/status")
    @Authorize(code = "SystemUser.Status")
    public RestMessage status(String id, Integer status) {
        return userService.updateStatus(id, ConvertHelper.toBool(status));
    }

    @ResponseBody
    @PostMapping("/modify-password")
    public RestMessage modifyPassword(String currentPassword, String newPassword) {
        return userService.modifyPassword(getCurrentUser().getId(), currentPassword, newPassword);
    }

    @ResponseBody
    @PostMapping("/revise-password")
    @Authorize(code = "SystemUser.RevisePassword")
    public RestMessage revisePassword(String userIds, String password) {
        return userService.revisePassword(userIds, password);
    }

    /**
     * 删除用户
     */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize(code = "SystemUser.Delete")
    public RestMessage delete(String ids) {
        return userService.delete(ids);
    }

    /**
     * 导出用户
     */
    @GetMapping("/export")
    @Authorize(code = "SystemUser.Export")
    public void export(UserFilter filter) {
        ExcelHelper.exportWeb(userService.selectList(filter), SysUser.class, "系统用户");
    }
    //endregion
}
